package quangtqServlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "DowloadListServlet", urlPatterns = {"/DowloadListServlet"})
public class DowloadListServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String fromUser = "dungna@srs.com";
        String userName = "lamhnh";
        String sentDate = "10/16/2010 8:52:20 PM";

        if (fromUser.indexOf(userName) < 0) {
            String headerFromUser = fromUser.substring(0, fromUser.indexOf("@"));
            int referenceMailID = 0;
            try {
                Connection con = helper.ConnectionHelper.GetConnection();
                String sql = "SELECT MailID FROM Mails WHERE UserName = ? AND SentDate = ?";
                PreparedStatement p = con.prepareStatement(sql);
                p.setString(1, headerFromUser);
                p.setString(2, sentDate);
                ResultSet rs = p.executeQuery();

                if (rs.next()) {
                    referenceMailID = rs.getInt("MailID");
                }
            } catch (SQLException e) {
                System.out.println("SQL Exception: " + e.toString());
            }

            out.print(request.getServletContext().getRealPath("/files/" + referenceMailID));
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
